﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using ZingFrameworkCore.Common.Cache;
using ZingFrameworkCore.Common.Http;

namespace ZingFrameworkCore.Common.Client
{
    public class IpHelper
    {
        public static string Ip => HttpContext.Current.Connection.RemoteIpAddress.ToString();
        public static IpLocation IpLocation
        {
            get
            {
                var httpRequester = new HttpRequester();
                var result = new IpLocation() { Ip = Ip };
                try
                {
                    var apiURL = $"https://whois.pconline.com.cn/ipJson.jsp?ip={Ip}&json=true";
                    var response =  httpRequester.Request(new HttpItem() { URL = apiURL }).ResponseData.ToObject<dynamic>();
                    result.Provice = response.pro;
                    result.City = response.city;
                }
                catch
                {
                    return result;
                }
                return result;
            }
        }
    }
    public class IpLocation
    {
        public string Ip { get; set; }
        public string Provice { get; set; }
        public string City { get; set; }

        public override string ToString()
        {
            return $"{Provice} {City}";
        }
    }
}
